package com.hhwy.framework.code.generation.util;

import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 选取配置文件
 * <b>类 名 称：</b>PropertyUtil<br/>
 * <b>类 描 述：</b><br/>
 * <b>创 建 人：</b>zhangbaojian<br/>
 * <b>修 改 人：</b>zhangbaojian<br/>
 * <b>修改时间：</b>2014年8月6日 上午9:37:12<br/>
 * <b>修改备注：</b><br/>
 * @version 1.0.0<br/>
 */
public class PropertyUtil {

	public static final Logger log = LoggerFactory.getLogger(PropertyUtil.class);
	public static Properties properties = null;
	public static String config = "/codeConfig.properties";
	
	public static Properties initProperties(){
		if(properties == null){
			properties = new Properties();
			InputStream in = PropertyUtil.class.getResourceAsStream(config);
			if(in != null){
				try {
					properties.load(in);
					log.info("加载codeConfig.properties配置文件成功");
				} catch (Exception e) {
					log.error("加载codeConfig.properties失败", e);
				}
			} else {
				log.info("codeConfig.properties配置文件不存在，加载db.properties");
				config = null;
				in = PropertyUtil.class.getResourceAsStream("/db.properties");
				try {
					properties.load(in);
					log.info("加载db.properties配置文件成功");
				} catch (Exception e) {
					log.error("加载db.properties失败", e);
				}
				properties.setProperty("jdbc.driverClassName", properties.getProperty("ds.driver"));
				properties.setProperty("jdbc.url", properties.getProperty("ds.url"));
				properties.setProperty("jdbc.username", properties.getProperty("ds.userName"));
				properties.setProperty("jdbc.password", properties.getProperty("ds.password"));
				
				properties.setProperty("version", "1.0");
			}
			
		}
		return properties;
		
	}

	public static String getValue(String key){
		return properties.getProperty(key);
	}
	
	public static String setValue(String key, String value){
		return properties.getProperty(key);
	}

}
